package com.nic.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.nic.view.R;
import com.nic.view.util.Utility;
import com.nic.view.util.glide.GlideUtil;
import com.nic.view.widget.ZoomImageView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//大图展示
public class PhotoPreviewActivity extends VBaseActivity {
    public final static String IMG = "img";
    public final static String LIST = "list";
    private ViewPager viewPager;
    private ViewAdapter adapter;
    private RadioGroup radioGroup;
    private ImageView ivBack;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_photo_preview;
    }

    @Override
    public void initView() {
        setToolbarVisibility(View.GONE);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new ViewAdapter();
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) radioGroup.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        String img = getIntent().getStringExtra(IMG);//以逗号隔开的url
        if (!TextUtils.isEmpty(img)) {
            setData(Utility.getListByDou(img));
        }
        List<String> list = (List<String>) getIntent().getSerializableExtra(LIST);
        if (list != null) {
            setData(list);
        }
    }

    private void addRadioButton() {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setButtonDrawable(R.drawable.selector_dot);
        RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = getResources().getDimensionPixelSize(R.dimen.dimen_4);
        lp.rightMargin = getResources().getDimensionPixelSize(R.dimen.dimen_4);
        radioButton.setLayoutParams(lp);
        radioGroup.addView(radioButton);
    }

    @NonNull
    private ZoomImageView getZoomImageView(String url) {
        ZoomImageView zoomImageView = new ZoomImageView(this);
        zoomImageView.setScaleType(ImageView.ScaleType.MATRIX);
        zoomImageView.setBackgroundColor(0xFF000000);
        GlideUtil.getInstance().load(url, zoomImageView);
        return zoomImageView;
    }

    public void setData(List<String> urls) {
        if (urls == null || urls.size() <= 0) {
            return;
        }
        List<View> datas = new ArrayList<>();
        for (int i = 0; i < urls.size(); i++) {
            String path = urls.get(i);
            if (!TextUtils.isEmpty(path)) {
                ZoomImageView zoomImageView = getZoomImageView(path);
                datas.add(zoomImageView);
                addRadioButton();
            }

        }
        adapter.setDatas(datas);
        RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
        rb.setChecked(true);
    }

    class ViewAdapter extends PagerAdapter {
        private List<View> datas = new ArrayList<>();

        public ViewAdapter() {
        }

        public void setDatas(List<View> datas) {
            this.datas = datas;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = datas.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(datas.get(position));
        }
    }

    public static void jumpTo(Activity activity, String urls) {
        Intent intent = new Intent(activity, PhotoPreviewActivity.class);
        intent.putExtra(IMG, urls);
        activity.startActivity(intent);
    }

    public static void jumpTo(Activity activity, List<String> list) {
        Intent intent = new Intent(activity, PhotoPreviewActivity.class);
        intent.putExtra(LIST, (Serializable) list);
        activity.startActivity(intent);
    }
}
